package com.ruoyi.web.controller.vantController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.GalleryCollection;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.service.IGalleryCollectionService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/system/collection")
public class ApiGalleryCollectionController extends BaseController {

    @Autowired
    private IGalleryCollectionService galleryCollectionService;
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TokenUtils tokenUtils;

//    @GetMapping("/list")
//    public TableDataInfo list(GalleryCollection galleryCollection)
//    {
//        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
//        galleryCollection.setCustomerId(customerLogin.getCustomerId());
//        List<GalleryCollection> list = galleryCollectionService.selectGalleryCollectionList(galleryCollection);
//        return getDataTable(list);
//    }

    @PostMapping("/galleryByCustomerId")
    public Long galleryByCustomerId(){
        return galleryCollectionService.galleryByCustomerId();
    }


    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody GalleryCollection galleryCollection)
    {
        Long collectionId = null;
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        GalleryCollection galleryCollection1 = new GalleryCollection();
        galleryCollection1.setGalleryInfoId(galleryCollection.getGalleryInfoId());
        galleryCollection1.setGalleryInfoId(galleryCollection.getGalleryInfoId());
        List<GalleryCollection> galleryCollections = galleryCollectionService.selectGalleryCollectionList(galleryCollection1);
        for (GalleryCollection galleryCollection2:galleryCollections){
            collectionId = galleryCollection2.getCollectionId();
        }

        if (galleryCollections.size() >0){
            return toAjax(galleryCollectionService.deleteGalleryCollectionById(collectionId));
        }else {
            galleryCollection.setCustomerId(customerLogin.getCustomerId());
            galleryCollection.setGalleryInfoId(galleryCollection.getGalleryInfoId());
            return toAjax(galleryCollectionService.insertGalleryCollection(galleryCollection));
        }
    }
}
